package com.bgs.travelapp.card.service.impl;

import com.bgs.travelapp.card.entity.CardDetailsEntity;
import com.bgs.travelapp.card.entity.CardEntity;
import com.bgs.travelapp.card.mapper.CardDetailsMapper;
import com.bgs.travelapp.card.mapper.CardMapper;
import com.bgs.travelapp.card.service.CardDetailsService;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import com.bgs.travelapp.result.CommonResult;
import com.bgs.travelapp.role.entity.RoleEntity;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * gpd
 * 会员卡详情修改功能 （后端）
 * 2022-07-25
 */
@Service
public class CardDetailsServiceImpl implements CardDetailsService {

    @Resource
    CardDetailsMapper cardDetailsMapper;

    @Resource
    CardMapper cardMapper;


    /**
     * 分月全查+模糊查（会员卡名称） （后端---会员卡详情）
     * gpd 2022-08-02 17:09
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult getCardDetailsByPage(PageRequest pageRequest, String cardName) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(CardDetailsEntity.class);
        example.createCriteria().andLike("cardName", "%" + cardName + "%").andEqualTo("delFlag", 0);
        PageResult pageResult = MyPageHelper.pageInfo(cardDetailsMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 新增会员卡详情  （后端---会员卡详情）
     * gpd 2022-08-02 17:14
     * @param cardDetailsEntity
     * @return
     */
    @Override
    public Integer saveCardDetails(CardDetailsEntity cardDetailsEntity) {
        cardDetailsEntity.setDelFlag(0);
        return cardDetailsMapper.insertSelective(cardDetailsEntity);
    }

    /**
     * 查询会员卡类型 （后端---会员卡详情）
     * gpd 2022-08-02 17:09
     * @return
     */
    @Override
    public List<CardEntity> getSelectCard() {
        Example example = new Example(CardEntity.class);
        example.createCriteria().andEqualTo("delFlag", 0);
        return cardMapper.selectByExample(example);
    }

    /**
     * 新增修改校验方法--查询月份是否重复录入 （后端---会员卡详情）
     * gpd 2022-08-02 17:14
     * @param purchaseMonth
     * @param cardId
     * @return
     */
    @Override
    public List<CardDetailsEntity> selectCardDetailsPurchaseMonth(Integer purchaseMonth, Integer cardId) {
        CardDetailsEntity cardDetailsEntity = new CardDetailsEntity();
        cardDetailsEntity.setCardId(cardId);
        cardDetailsEntity.setPurchaseMonth(purchaseMonth);
        return cardDetailsMapper.select(cardDetailsEntity);
    }

    /**
     * 修改会员卡详情 （后端---会员卡详情）
     * gpd 2022-08-02 17:15
     * @param cardDetailsEntity
     * @return
     */
    @Override
    public Integer updateCardDetails(CardDetailsEntity cardDetailsEntity) {
        return cardDetailsMapper.updateByPrimaryKeySelective(cardDetailsEntity);
    }

    /**
     * 删除会员卡详情 （后端---会员卡详情）
     * gpd 2022-08-02 17:15
     * @param id
     * @return
     */
    @Override
    public Integer deleteCardDetails(Integer id) {
        CardDetailsEntity cardDetailsEntity = new CardDetailsEntity();
        cardDetailsEntity.setId(id);
        cardDetailsEntity.setDelFlag(1);
        return cardDetailsMapper.updateByPrimaryKeySelective(cardDetailsEntity);
    }

}
